| Conditions | 1 |
| Paths | 1 |
| Total Lines | 173 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 14 | ], function($, CommentManager, CommentRouter) { |
||
| 15 | |||
| 16 | 'use strict'; |
||
| 17 | |||
| 18 | return { |
||
| 19 | |||
| 20 | defaults: { |
||
| 21 | translations: { |
||
| 22 | headline: 'sulu_comment.comments', |
||
| 23 | published: 'sulu_comment.comment.published', |
||
| 24 | unpublished: 'sulu_comment.comment.unpublished' |
||
| 25 | } |
||
| 26 | }, |
||
| 27 | |||
| 28 | header: function() { |
||
| 29 | var buttons = { |
||
| 30 | save: {}, |
||
| 31 | edit: { |
||
| 32 | options: { |
||
| 33 | dropdownItems: { |
||
| 34 | delete: { |
||
| 35 | options: { |
||
| 36 | callback: this.delete.bind(this) |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | }, |
||
| 42 | state: { |
||
| 43 | parent: 'toggler', |
||
| 44 | options: { |
||
| 45 | title: this.translations.unpublished |
||
| 46 | } |
||
| 47 | } |
||
| 48 | }; |
||
| 49 | |||
| 50 | // unsafe check is used because rest-api returns as string |
||
| 51 | if (this.data.state == 1) { |
||
| 52 | buttons.state = { |
||
| 53 | parent: 'toggler-on', |
||
| 54 | options: { |
||
| 55 | title: this.translations.published, |
||
| 56 | callback: this.toggleState.bind(this) |
||
| 57 | } |
||
| 58 | }; |
||
| 59 | } |
||
| 60 | |||
| 61 | return { |
||
| 62 | title: function() { |
||
| 63 | return this.translations.headline; |
||
| 64 | }.bind(this), |
||
| 65 | |||
| 66 | tabs: { |
||
| 67 | url: '/admin/content-navigations?alias=comments', |
||
| 68 | options: { |
||
| 69 | data: function() { |
||
| 70 | return this.sandbox.util.extend(false, {}, this.data); |
||
| 71 | }.bind(this) |
||
| 72 | }, |
||
| 73 | componentOptions: { |
||
| 74 | values: this.data |
||
| 75 | } |
||
| 76 | }, |
||
| 77 | |||
| 78 | toolbar: { |
||
| 79 | buttons: buttons |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | }, |
||
| 83 | |||
| 84 | loadComponentData: function() { |
||
| 85 | var promise = $.Deferred(); |
||
| 86 | |||
| 87 | if (!this.options.id) { |
||
| 88 | promise.resolve({}); |
||
| 89 | |||
| 90 | return promise; |
||
| 91 | } |
||
| 92 | CommentManager.load(this.options.id).done(function(data) { |
||
| 93 | promise.resolve(data); |
||
| 94 | }); |
||
| 95 | |||
| 96 | return promise; |
||
| 97 | }, |
||
| 98 | |||
| 99 | initialize: function() { |
||
| 100 | this.bindCustomEvents(); |
||
| 101 | }, |
||
| 102 | |||
| 103 | bindCustomEvents: function() { |
||
| 104 | this.sandbox.on('sulu.header.back', this.toList.bind(this)); |
||
| 105 | this.sandbox.on('sulu.tab.dirty', this.enableSave.bind(this)); |
||
| 106 | this.sandbox.on('sulu.toolbar.save', this.save.bind(this)); |
||
| 107 | this.sandbox.on('sulu.tab.data-changed', this.setData.bind(this)); |
||
| 108 | |||
| 109 | // state-toggler |
||
| 110 | this.sandbox.on('husky.toggler.sulu-toolbar.changed', this.toggleState.bind(this)); |
||
| 111 | }, |
||
| 112 | |||
| 113 | toList: function() { |
||
| 114 | CommentRouter.toList(); |
||
| 115 | }, |
||
| 116 | |||
| 117 | toggleState: function() { |
||
| 118 | this.sandbox.emit('sulu.header.toolbar.item.loading', 'save'); |
||
| 119 | |||
| 120 | // unsafe check is used because rest-api returns as string |
||
| 121 | if (this.data.state == 0) { |
||
| 122 | this.sandbox.emit('sulu.header.toolbar.button.set', 'state', {title: this.translations.published}); |
||
| 123 | |||
| 124 | return CommentManager.publish(this.data.id).done(function(data) { |
||
| 125 | this.data.state = data.state; |
||
| 126 | this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false); |
||
| 127 | }.bind(this)); |
||
| 128 | } |
||
| 129 | |||
| 130 | this.sandbox.emit('sulu.header.toolbar.button.set', 'state', {title: this.translations.unpublished}); |
||
| 131 | |||
| 132 | return CommentManager.unpublish(this.data.id).done(function(data) { |
||
| 133 | this.data.state = data.state; |
||
| 134 | this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false); |
||
| 135 | }.bind(this)); |
||
| 136 | }, |
||
| 137 | |||
| 138 | delete: function() { |
||
| 139 | this.sandbox.emit('sulu.header.toolbar.item.loading', 'edit'); |
||
| 140 | |||
| 141 | CommentManager.delete(this.data.id).done(function() { |
||
| 142 | this.sandbox.emit('sulu.header.toolbar.item.enable', 'edit', false); |
||
| 143 | CommentRouter.toList(); |
||
| 144 | }.bind(this)); |
||
| 145 | }, |
||
| 146 | |||
| 147 | save: function(action) { |
||
| 148 | this.loadingSave(); |
||
| 149 | |||
| 150 | this.saveTab().then(function(data) { |
||
| 151 | this.afterSave(action, data); |
||
| 152 | }.bind(this)); |
||
| 153 | }, |
||
| 154 | |||
| 155 | setData: function(data) { |
||
| 156 | this.data = data; |
||
| 157 | }, |
||
| 158 | |||
| 159 | saveTab: function() { |
||
| 160 | var promise = $.Deferred(); |
||
| 161 | |||
| 162 | this.sandbox.once('sulu.tab.saved', function(savedData) { |
||
| 163 | this.setData(savedData); |
||
| 164 | |||
| 165 | promise.resolve(savedData); |
||
| 166 | }.bind(this)); |
||
| 167 | |||
| 168 | this.sandbox.emit('sulu.tab.save'); |
||
| 169 | |||
| 170 | return promise; |
||
| 171 | }, |
||
| 172 | |||
| 173 | enableSave: function() { |
||
| 174 | this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false); |
||
| 175 | }, |
||
| 176 | |||
| 177 | loadingSave: function() { |
||
| 178 | this.sandbox.emit('sulu.header.toolbar.item.loading', 'save'); |
||
| 179 | }, |
||
| 180 | |||
| 181 | afterSave: function(action, data) { |
||
| 182 | this.sandbox.emit('sulu.header.toolbar.item.disable', 'save', true); |
||
| 183 | this.sandbox.emit('sulu.header.saved', data); |
||
| 184 | } |
||
| 185 | }; |
||
| 186 | }); |
||
| 187 |